﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
传送
 */
public class TransferDevice : Weapon {


	private float maxBuffer;//最大缓冲
	private float currBuffer;//当前缓冲
	private float everyBuffer;//每帧缓冲
	private float expendBuffer;//每次消耗
	private float tempBuffer;//缓冲，用于恢复和消耗血量
	private float transferDistance;//传送距离


	private GameObject gun;//绑定的枪
	private GameObject target;

	// Use this for initialization
	void Start () {
		Init();
		AttackTargetManager.SendMsgToDevice += ChangeState;
	}

	private void Init()
	{
		maxBuffer = 1000;
		currBuffer = maxBuffer;
		everyBuffer = 1;
		expendBuffer = 300;
		transferDistance = 20;
	}
	
	// Update is called once per frame
	void Update () {
		RecoverBuffer();
	}


	void Transfer()
	{
		target.transform.position += target.transform.up * transferDistance;

	}


	public void RecoverBuffer()
	{
		tempBuffer = currBuffer + everyBuffer;
		if(tempBuffer < maxBuffer)
		{
			currBuffer = tempBuffer;
		}
		else
		{
			currBuffer = maxBuffer;
		}
		tempBuffer = 0;
		print(currBuffer);
	}

	public bool SendBuffer()
	{
		tempBuffer = currBuffer - expendBuffer;
		if(tempBuffer > 0)
		{
			currBuffer = tempBuffer;
			return true;
		}
		else
		{
			return false;
		}
		
	}


	public void SetGun(GameObject gun)
	{
		this.gun = gun;
	}


	public void SetTarget(GameObject target)
	{
		this.target = target;
	}



	private void ChangeState(string weaponName, string gunName)
	{
		print(weaponName);
		if(weaponName == "TransferDevice" && gunName == gun.gameObject.name)
		{
			print("接收到了");
			if(SendBuffer() == true)
			{
				Transfer();
			}
		}
	}
}
